<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="../templates/ui.xhtml">

    <ui:define name="content">

        <h1 class="title ui-widget-header ui-corner-all">Dynamic Image Streaming</h1>
        <div class="entry">
            <p>DynamicImage is used to present images that are created programatically at runtime or images stored in database.</p>

            <h3>JFreeChart on-the-fly</h3>
            <p:graphicImage id="chart" value="#{dynamicImageController.chart}" />

            <h3>Barcode on-the-fly</h3>
            <p:graphicImage id="barcode" value="#{dynamicImageController.barcode}" />
            
            <h3>GraphicText on-the-fly</h3>
            <p:graphicImage id="text" value="#{dynamicImageController.graphicText}" />

            <h3>Source</h3>
            <p:tabView>
                <p:tab title="dynamicImage.xhtml">
                    <pre name="code" class="xml">
&lt;h3&gt;JFreeChart on-the-fly&lt;/h3&gt;
&lt;p:graphicImage value="\#{dynamicImageController.chart}" /&gt;

&lt;h3&gt;Barcode on-the-fly&lt;/h3&gt;
&lt;p:graphicImage value="\#{dynamicImageController.barcode}" /&gt;
    
&lt;h3&gt;GraphicText on-the-fly&lt;/h3&gt;
&lt;p:graphicImage value="\#{dynamicImageController.graphicText}" /&gt;
                    </pre>
                </p:tab>

                <p:tab title="DynamicImageController.java">
<pre name="code" class="java">
package org.primefaces.examples.view;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;

import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

public class DynamicImageController {

	private StreamedContent graphicText;
	
	private StreamedContent barcode;
	
	private StreamedContent chart;

	public DynamicImageController() {
        try {
            //Graphic Text
            BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bufferedImg.createGraphics();
            g2.drawString("This is a text", 0, 10);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bufferedImg, "png", os);
            graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png"); 

            //Chart
            JFreeChart jfreechart = ChartFactory.createPieChart("Turkish Cities", createDataset(), true, true, false);
            File chartFile = new File("dynamichart");
            ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
            chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");

            //Barcode
			File barcodeFile = new File("dynamicbarcode");
			BarcodeImageHandler.saveJPEG(BarcodeFactory.createCode128("PRIMEFACES"), barcodeFile);
			barcode = new DefaultStreamedContent(new FileInputStream(barcodeFile), "image/jpeg");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public StreamedContent getBarcode() {
		return barcode;
	}

    public StreamedContent getGraphicText() {
        return graphicText;
    }
		
	public StreamedContent getChart() {
		return chart;
	}
	
	private PieDataset createDataset() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("Istanbul", new Double(45.0));
		dataset.setValue("Ankara", new Double(15.0));
		dataset.setValue("Izmir", new Double(25.2));
		dataset.setValue("Antalya", new Double(14.8));

		return dataset;
	}
}

            </pre>

                </p:tab>
            </p:tabView>

        </div>

    </ui:define>
</ui:composition>